/*
 * HistoryProvider.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.history;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class HistoryProvider {
	private final static String TAG = "learningwords";
	
	private Context context;

	private static final int DATABASE_VERSION = 2;
	private static final String DATABASE_NAME = "history.db";
	private static final String HISTORY_TABLE_NAME = "history";
	public static final String ID = "_id";
	public static final String SEARCH_STRING = "search_string";
	public static final String SEARCH_LANGUAGE = "search_language";
	public static final String SEARCH_WAY = "search_way";
	private static final String NOT_KNOWN = "not_known";

	private HistoryOpenHelper hoh;
	
	public HistoryProvider( Context context ) {
		this.context = context;
		hoh = new HistoryOpenHelper( context, null);
	}

	public long insert(int searchWay, String searchString, int searchLanguage) {
		SQLiteDatabase db = hoh.getWritableDatabase();
		ContentValues cv = new ContentValues();
		// cv.put(ID, 0);
		cv.put(SEARCH_WAY, searchWay);
		cv.put(SEARCH_STRING, searchString);
		cv.put(SEARCH_LANGUAGE, searchLanguage);
		long rowId = db.insert(HISTORY_TABLE_NAME, NOT_KNOWN, cv);
		db.close();
		return rowId;
	}
	
	public int deleteOld( int searchWay, String searchString, int searchLanguage ) {
		SQLiteDatabase db = hoh.getWritableDatabase();
		
		String whereArgs[] = { Integer.valueOf(searchWay).toString(), searchString, Integer.valueOf(searchLanguage).toString() };
		int count = db
				.delete( HISTORY_TABLE_NAME, SEARCH_WAY + " = ? AND " + SEARCH_STRING + " = ? AND " + SEARCH_LANGUAGE + " = ? ", whereArgs);
		db.close();
		// Log.d( TAG, "deleteOld: deleted " + count + " entries.");
		return count;
	}

	public Cursor query() {
		SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
		builder.setTables(HISTORY_TABLE_NAME);
		Cursor cursor = builder.query(hoh.getReadableDatabase(), null, null,
				null, null, null, ID + " DESC");
		return cursor;
	}

	public class HistoryRow {
		public int id;
		public int searchWay;
		public String searchString;
		public int searchLanguage;
	}

	public HistoryRow getNextRow(Cursor cursor) {
		if (cursor.isBeforeFirst())
			cursor.moveToFirst();
		else
			cursor.moveToNext();

		if (cursor.isAfterLast())
			return null;

		int idColumn = cursor.getColumnIndex(ID);
		int searchWayColumn = cursor.getColumnIndex(SEARCH_WAY);
		int searchStringColumn = cursor.getColumnIndex(SEARCH_STRING);
		int searchLanguageColumn = cursor.getColumnIndex(SEARCH_LANGUAGE);

		HistoryRow hr = new HistoryRow();
		hr.id = cursor.getInt(idColumn);
		hr.searchWay = cursor.getInt(searchWayColumn);
		hr.searchString = cursor.getString(searchStringColumn);
		hr.searchLanguage = cursor.getInt(searchLanguageColumn);

		return hr;

	}
	
	public void clearDatabase() {
		hoh.onUpgrade( hoh.getWritableDatabase(), 0, 0 );
	}

	public class HistoryOpenHelper extends SQLiteOpenHelper {

		private static final String HISTORY_TABLE_CREATE = "CREATE TABLE "
			+ HISTORY_TABLE_NAME + " (" + ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + SEARCH_WAY
			+ " INTEGER, " + SEARCH_STRING + " TEXT, " + SEARCH_LANGUAGE
			+ " INTEGER )";
		
		public HistoryOpenHelper(Context context, CursorFactory factory) {
			super(context, DATABASE_NAME, factory, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(HISTORY_TABLE_CREATE);
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ". Destroys all old data.");
			db.execSQL("DROP TABLE IF EXISTS " + HISTORY_TABLE_NAME);
			onCreate(db);
		}

	}
}
